//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action;

import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.strategy.IModelStrategy;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.mdiy.biz.IConfigBiz;
import net.mingsoft.mdiy.biz.IModelBiz;
import net.mingsoft.mdiy.entity.ConfigEntity;
import net.mingsoft.mdiy.entity.ModelEntity;
import net.mingsoft.mdiy.util.ConfigUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-自定义模块接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/mdiy/config/data"})
public class ConfigDataAction extends BaseAction {
    @Autowired
    private IConfigBiz configBiz;
    @Autowired
    private IModelBiz modelBiz;
    @Autowired
    private IModelStrategy modelStrategy;

    public ConfigDataAction() {
    }

    @ApiIgnore
    @GetMapping({"/form"})
    public String form(HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        return "/mdiy/config/data/form";
    }

    @ApiOperation("配置数据获取接口")
    @GetMapping({"/get"})
    @ResponseBody
    public ResultData get(HttpServletResponse response, HttpServletRequest request) {
        String modelId = BasicUtil.getString("modelId");
        if (StringUtils.isEmpty(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else {
            ModelEntity modelEntity = (ModelEntity)this.modelBiz.getById(modelId);
            if (modelEntity == null) {
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.id")}));
            } else if (!this.getPermissions("mdiy:configData:view", "mdiy:configData:" + modelEntity.getModelName() + ":view")) {
                return ResultData.build().error("没有权限!");
            } else {
                ConfigEntity configEntity = ConfigUtil.getEntity(modelEntity.getModelName());
                return configEntity == null ? ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("config.name")})) : ResultData.build().success(JSONUtil.parseObj(configEntity.getConfigData()));
            }
        }
    }

    @ApiOperation("更新自定义配置")
    @LogAnn(
            title = "更新自定义配置",
            businessType = BusinessTypeEnum.UPDATE
    )
    @PostMapping({"/update"})
    @ResponseBody
    public ResultData update(HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> map = BasicUtil.assemblyRequestMap();
        String modelId = map.get("modelId").toString();
        if (StringUtils.isEmpty(modelId)) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("model.id")}));
        } else {
            ModelEntity modelEntity = (ModelEntity)this.modelBiz.getById(modelId);
            if (modelEntity == null) {
                return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("model.id")}));
            } else if (!this.getPermissions("mdiy:configData:update", "mdiy:configData:" + modelEntity.getModelName() + ":update")) {
                return ResultData.build().error("没有权限!");
            } else {
                ConfigEntity configEntity = ConfigUtil.getEntity(modelEntity.getModelName());
                if (configEntity == null) {
                    return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("config.name")}));
                } else {
                    configEntity.setConfigData(JSONUtil.toJsonStr(map));
                    this.configBiz.updateById(configEntity);
                    return ResultData.build().success(configEntity);
                }
            }
        }
    }

    @ApiOperation("获取自定义配置中的key指定value值接口")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "configName",
            value = "配置名称",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "key",
            value = "配置的key值",
            required = true,
            paramType = "query"
    )})
    @GetMapping({"/getMap"})
    @ResponseBody
    public ResultData getMap(String configName, String key, HttpServletResponse response, HttpServletRequest request) {
        return StringUtils.isBlank(key) ? ResultData.build().success(ConfigUtil.getMap(configName)) : ResultData.build().success(ConfigUtil.getString(configName, key));
    }
}
